
import { _decorator, Component, Node } from 'cc';
import { GameData, PhaseTransitionType } from './GameData';
import { resManger } from '../tools/ResManger';
import { buildingPlatform_config, bullet_config, ConfigKey, current_config, gunRandomLevel_config, gunRandomSpend_config, gunResolve_config, MainGun_Item, monster_base_config, monster_Refresh_config, moveLine_config, prop_config, secondaryGun_config, secondGun_specialSkill_config, secretShop_config, skin_config, tasteMainGun_config } from '../manager/AppControll';
import { Tools } from '../tools/Tools';
import { unlockType } from '../pop/Skin';
import { randomGunWeight } from '../pop/SecondGunRandom';
const { ccclass, property } = _decorator;


@ccclass('DataHelper')
export class DataHelper {
    static GetGunResolveConfigByQuality(quality: number): gunResolve_config {
        let result = null;
        let configL = resManger.configJson["gunResolve_config"]
        if (configL) {
            for (const key in configL) {
                if (Object.prototype.hasOwnProperty.call(configL, key)) {
                    const element: gunResolve_config = configL[key];
                    if (element.gunQuality == quality) {
                        result = element;
                    }
                }
            }
        }
        return result;
    }

    static GetGunAtkProportionByQuality(quality: number): number {
        let proportion: string = null;
        switch (quality) {
            case 0:
                proportion = DataHelper.GetConfigByKey(ConfigKey.grayGunAtkProportion).value;
                break;
            case 1:
                proportion = DataHelper.GetConfigByKey(ConfigKey.greenGunAtkProportion).value;
                break;
            case 2:
                proportion = DataHelper.GetConfigByKey(ConfigKey.blueGunAtkProportion).value;
                break;
            case 3:
                proportion = DataHelper.GetConfigByKey(ConfigKey.purpleGunAtkProportion).value;
                break;
            case 4:
                proportion = DataHelper.GetConfigByKey(ConfigKey.yellowGunAtkProportion).value;
                break;
            case 5:
                proportion = DataHelper.GetConfigByKey(ConfigKey.redGunAtkproPortion).value;
                break;
        }
        return Number.parseFloat(proportion);
    }

    static GetBuildingPlatformConfigByLv(lv: number): buildingPlatform_config {
        let result = null;
        let configL = DataHelper.GetBuildingPlatformConfig();
        if (configL) {
            for (const key in configL) {
                if (Object.prototype.hasOwnProperty.call(configL, key)) {
                    const element: buildingPlatform_config = configL[key];
                    if (element.buildLevel == lv) {
                        result = element;
                    }
                }
            }
        }
        return result;
    }

    static GetBuildingPlatformConfigById(id: number): buildingPlatform_config {
        let result = null;
        if (id + "" in resManger.configJson["buildingPlatform_config"]) {
            result = resManger.configJson["buildingPlatform_config"][id + ""]
        }
        return result;
    }


    static GetBuildingPlatformConfig() {
        return resManger.configJson["buildingPlatform_config"];
    }

    static GunRandomSpendConfigByLv(lv): gunRandomSpend_config {
        let result = null;
        let configL = DataHelper.GetGunRandomSpendConfig();
        if (configL) {
            for (const key in configL) {
                if (Object.prototype.hasOwnProperty.call(configL, key)) {
                    const element: gunRandomSpend_config = configL[key];
                    if (element.level == lv) {
                        result = element;
                    }
                }
            }
        }
        return result;
    }

    static GetGunRandomSpendConfig() {
        return resManger.configJson["gunRandomSpend_config"];
    }

    static GetRandomLvelConfigByLv(lv: number, lt: randomGunWeight[]) {
        let config = DataHelper.GetRandomLevelConfig();
        for (const key in config) {
            if (Object.prototype.hasOwnProperty.call(config, key)) {
                const element = config[key];
                if (element["level_" + lv]) {
                    lt.push({ key: element.counterGun, weight: element["level_" + lv] });
                }
            }
        }
    }

    static GetRandomLevelConfig() {
        return resManger.configJson["gunRandomLevel_config"];
    }

    static GetMonsterRefreshConfig(): monster_Refresh_config {
        let count = GameData.playerData.KillMonsters;
        let ltConfig = resManger.configJson["monster_Refresh_config"];
        let result: monster_Refresh_config = null;
        if (GameData.playerData.PhaseTransition == PhaseTransitionType.lock) {
            result = DataHelper.GetMonsterRefreshConfigById(GameData.playerData.CurStage)
            return result;
        } else if (GameData.playerData.PhaseTransition == PhaseTransitionType.automatic) {
            let max = 0;
            let id = 0;
            for (const key in ltConfig) {
                if (Object.prototype.hasOwnProperty.call(ltConfig, key)) {
                    const element: monster_Refresh_config = ltConfig[key];
                    if (element.maxKillMonsters >= max) {
                        max = element.maxKillMonsters;
                    }
                    if (element.stageID >= id) {
                        id = element.stageID;
                    }
                    if (element.minKillMonsters <= count && element.maxKillMonsters >= count) {
                        GameData.playerData.CurStage = element.stageID;
                        result = element;
                        break;
                    }
                }
            }
            if (count >= max) {
                result = DataHelper.GetMonsterRefreshConfigById(id);
                GameData.playerData.CurStage = result.stageID;
            }

            if (result == null) {
                result = DataHelper.GetMonsterRefreshConfigById(id);
                GameData.playerData.CurStage = result.stageID;
            }
            return result;
        }
    }

    static GetLevelModelRefreshById(id: number) {
        let result = null;
        if (id + "" in resManger.configJson["level_monster_Refresh_config"]) {
            result = resManger.configJson["level_monster_Refresh_config"][id + ""]
        }
        return result;
    }

    static GetTurntableConfig() {
        return resManger.configJson["turntable_config"];
    }

    static GetSecretShopConfig() {
        return resManger.configJson["secretShop_config"];
    }

    static GetSecretShopConfigById(id): secretShop_config {
        let result = null
        if (id + "" in resManger.configJson["secretShop_config"]) {
            result = resManger.configJson["secretShop_config"][id];
        }
        return result;
    }

    static GetPropConfigById(id): prop_config {
        let result = null
        if (id + "" in resManger.configJson["prop_config"]) {
            result = resManger.configJson["prop_config"][id];
        }
        return result;
    }

    static GetLevelModelRefresh() {
        return resManger.configJson["level_monster_Refresh_config"];
    }

    static GetSkin_config() {
        return resManger.configJson["skin_config"];
    }

    static GetSkin_configById(id: number): skin_config {
        let result = null
        if (id + "" in resManger.configJson["skin_config"]) {
            result = resManger.configJson["skin_config"][id];
        }
        return result;
    }

    static GetMonsterRefreshByAuto(): monster_Refresh_config {
        let count = GameData.playerData.KillMonsters;
        let result: monster_Refresh_config = null;
        let ltConfig = resManger.configJson["monster_Refresh_config"];
        let max = 0;
        let id = 0;
        for (const key in ltConfig) {
            if (Object.prototype.hasOwnProperty.call(ltConfig, key)) {
                const element: monster_Refresh_config = ltConfig[key];
                if (element.maxKillMonsters >= max) {
                    max = element.maxKillMonsters;
                }
                if (element.stageID >= id) {
                    id = element.stageID;
                }
                if (element.minKillMonsters <= count && element.maxKillMonsters >= count) {
                    result = element;
                    break;
                }
            }
        }
        if (count >= max) {
            result = DataHelper.GetMonsterRefreshConfigById(id);
        }
        return result;
    }

    static GetMonsterRefreshConfigById(id: number) {
        let result = null
        if (id + "" in resManger.configJson["monster_Refresh_config"]) {
            result = resManger.configJson["monster_Refresh_config"][id];
        }
        return result;
    }

    static GetMonsterBaseConfigById(id: number): monster_base_config {
        return resManger.configJson["monster_base_config"][id];
    }

    static GetMoveLineConfigById(id: number): moveLine_config {
        return resManger.configJson["moveLine_config"][id];
    }

    static GetAttack(): MainGun_Item {
        return resManger.configJson["MainGun__config"]["20001"][GameData.playerData.MGAttackLv + ""];
    }

    static GetAutoSpeed(): MainGun_Item {
        let result = null;
        if (GameData.playerData.MGAutomaticAttackLv + "" in resManger.configJson["MainGun__config"]['2011']) {
            result = resManger.configJson["MainGun__config"]['2011'][GameData.playerData.MGAutomaticAttackLv + ""];
        }
        return result;
    }

    static GetAutoSpeedByLv(lv: number): MainGun_Item {
        let result = null;
        if (lv + "" in resManger.configJson["MainGun__config"]['2011']) {
            result = resManger.configJson["MainGun__config"]['2011'][lv + ""];
        }
        return result;
    }

    static GetCriticalHitRate(): MainGun_Item {
        return resManger.configJson["MainGun__config"]["20004"][GameData.playerData.MGCriticalHitRateLv + ""];
    }

    static GetCriticalHitDamage(): MainGun_Item {
        return resManger.configJson["MainGun__config"]["20007"][GameData.playerData.MGCriticalHitDamageLv + ""];
    }

    static GetDamageBoost(): MainGun_Item {
        return resManger.configJson["MainGun__config"]["20010"][GameData.playerData.MGDamageBoostLv + ""];
    }

    static GetAttackByLv(lv: Number): MainGun_Item {
        let result = null;
        if (lv + "" in resManger.configJson["MainGun__config"]["20001"]) {
            result = resManger.configJson["MainGun__config"]["20001"][lv + ""]
        }
        return result;
    }

    static GetCriticalHitRateByLv(lv: Number): MainGun_Item {
        let result = null;
        if (lv + "" in resManger.configJson["MainGun__config"]["20004"]) {
            result = resManger.configJson["MainGun__config"]["20004"][lv + ""]
        }
        return result;
    }

    static GetCriticalHitDamageByLv(lv: Number): MainGun_Item {
        let result = null;
        if (lv + "" in resManger.configJson["MainGun__config"]["20007"]) {
            result = resManger.configJson["MainGun__config"]["20007"][lv + ""]
        }
        return result;
    }

    static GetDamageBoostByLv(lv: Number): MainGun_Item {
        let result = null;
        if (lv + "" in resManger.configJson["MainGun__config"]["20010"]) {
            result = resManger.configJson["MainGun__config"]["20010"][lv + ""]
        }
        return result;
    }

    static GetBulletConfig(id: number): bullet_config {
        return resManger.configJson["bullet_config"][id];
    }

    static GetSecondGunUnlockConfig() {
        return resManger.configJson["secondGun_unlock_config"];
    }

    static GetSpecialSkillConfig() {
        return resManger.configJson["secondGun_specialSkill_config"];
    }

    static GetSpecialSkillConfigById(id: number): secondGun_specialSkill_config {
        let result = null;
        if (id + "" in resManger.configJson["secondGun_specialSkill_config"]) {
            result = resManger.configJson["secondGun_specialSkill_config"][id + ""]
        }
        return result;
    }

    static GetSecondaryConfigByLv(lv: Number): secondaryGun_config {
        let result = null
        if (lv + "" in resManger.configJson["secondaryGun_config"]) {
            result = resManger.configJson["secondaryGun_config"][lv + ""]
        }
        return result;
    }

    static GetRandomGun(): secondGun_specialSkill_config {
        let lt: randomGunWeight[] = [];
        let result: number[][] = [];
        let level = 0
        if (GameData.playerData.randomLevel != null && GameData.playerData.randomLevel != undefined) {
            level = GameData.playerData.randomLevel;
        }
        DataHelper.GetRandomLvelConfigByLv(level, lt);

        let tempLt = [];
        lt.forEach(elemt => {
            tempLt = [];
            tempLt.push(elemt.key);
            tempLt.push(elemt.weight);
            result.push(tempLt)
        })
        let id = Tools.GetResultByWeightLt(result);
        return DataHelper.GetSpecialSkillConfigById(id);
    }

    static GetSecondLv(): number {
        let buildLv = GameData.playerData.craftingTableLv;
        let config = DataHelper.GetBuildingPlatformConfigByLv(buildLv);
        let result: number[][] = config.randomWeight;
        let lv = Tools.GetResultByWeightLt(result);
        return lv;
    }

    static GetSecondaryGunStructByUnlockId(id: number) {
        let index = GameData.playerData.UnLockSecondaryGun.indexOf(id);
        if (index != -1) {
            return GameData.playerData.SecondaryGun[index];
        } else {
            return null;
        }
    }

    static GetColorByLv(lv: number): current_config {
        let result = null
        if ("color00" + lv in resManger.configJson["current_config"]) {
            result = resManger.configJson["current_config"]["color00" + lv];
        }
        return result;
    }

    static GetConfigByKey(key: ConfigKey): current_config {
        let result = null
        if (key in resManger.configJson["current_config"]) {
            result = resManger.configJson["current_config"][key];
        }
        return result;
    }

    static GetTasteConfig() {
        return resManger.configJson["tasteMainGun_config"];
    }

    static GetSkinBuffDmg() {
        let arr = GameData.playerData.skin;
        let temp1 = 1;
        for (let i = 0; i < arr.length; i++) {
            let config = DataHelper.GetSkin_configById(arr[i]);
            if (config.unlockAttrType == unlockType.MoreDamage) {
                temp1 += config.unlockNum;
            }
        }
        return temp1;
    }

    static GetSkinBuffGold() {
        let arr = GameData.playerData.skin;
        let temp1 = 1;
        for (let i = 0; i < arr.length; i++) {
            let config = DataHelper.GetSkin_configById(arr[i]);
            if (config.unlockAttrType == unlockType.MoreGold) {
                temp1 += config.unlockNum;
            }
        }
        return temp1;
    }

    static GetSkinAttackInterval() {
        let arr = GameData.playerData.skin;
        let temp1 = 0;
        for (let i = 0; i < arr.length; i++) {
            let config = DataHelper.GetSkin_configById(arr[i]);
            if (config.unlockAttrType == unlockType.AttackInterval) {
                temp1 += config.unlockNum;
            }
        }
        return temp1;
    }

    static GetSignInConfig() {
        return resManger.configJson["signIn_config"];
    }

    static GetSkinFragmentCountById(id: number) {
        let arr = GameData.playerData.skinFragments;
        let res = null;
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].skinId == id) {
                res = arr[i];
            }
        }
        if (!res) return 0;
        return res.count;
    }
}